from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from . import models, myser


# Create your views here.


# 登录
class Login(APIView):
    def post(self, request):
        name = request.data.get('name')
        password = request.data.get('password')
        student = models.Student.objects.filter(username=name, password=password).first()
        res = {}
        if student:
            course = models.Course.objects.filter(student__username=name)
            course_ser = myser.CourseSer(course, many=True)
            # print(course,"__________________________")
            res['code'] = 200,
            res['message'] = "登陆成功",
            res['uid'] = student.id,
            res['course'] = course_ser.data
        else:
            res['code'] = 499,
            res['message'] = "信息有误"
        return Response(res)


# 添加课程
class AddCourse(APIView):
    def post(self, request):

        course = myser.CourseSer(data=request.data)
        res = {}
        if course.is_valid():
            course.save()
            res['code'] = 200,
            res['message'] = "添加成功"
        else:
            res['code'] = 499,
            res['message'] = "添加失败"
        return Response(res)


# 搜索
# class Search(APIView):
#     def post(self, request):
#         uid = request.data.get('uid')
#         sousuo = request.data.get('sousuo')
#
#         course = models.Course.objects.filter(title__contains=sousuo).first()


# 学生列表
class StudentList(APIView):
    def get(self, request):
        students = models.Student.objects.all()
        students_ser = myser.StudentSer(students, many=True)
        res = {
            'code': 200,
            'message': students_ser.data
        }
        return Response(res)


# 课程列表
class CourseList(APIView):
    def get(self, request):
        courses = models.Course.objects.all()
        courses_ser = myser.CourseSer(courses, many=True)
        res = {
            'code': 200,
            'message': courses_ser.data
        }
        return Response(res)


# 删除
class Del(APIView):
    def post(self, request):
        uid = request.data.get('uid')
        student = models.Student.objects.filter(id=uid).first()
        models.Course.objects.filter(student__id=uid).delete()
        res = {
            'code': 200,
            # 'message':""
        }
        return Response(res)


# 选课
class Index(APIView):
    def post(self, request):
        uid = request.data.get('uid')
        cid = request.data.get('cid')
        # print("uid:"+uid, "cid:"+cid,"++++++++++++++++++++++++++++++++++")

        course = models.Course.objects.filter(id=cid).first()
        student = models.Student.objects.filter(id=uid).first()

        if student in course.student.all():
            return Response({
                'code': 499,
                'message': "已关注"
            })
        print("aaaaaa")
        course.student.add(student)
        print("bbbbbbb")
        return Response({
            'code': 200,
            'message': "关注成功"
        })
